package com.zkc.bootnew.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkc.bootnew.webservice.webuserservice;
import com.zkc.springbootnew.bean.Contact_user;

import com.zkc.springbootnew.utils.Dto;
import com.zkc.springbootnew.utils.DtoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @Resource
    webuserservice webuserservice;


    @GetMapping(value = "/userall")
    @ApiOperation(value = "查询全部用户")
    public Dto getUserAll(@ApiParam(value = "当前页") @RequestParam("pagenum") Integer pagenum, @ApiParam(value = "页大小") @RequestParam("pagesize") Integer pagesize) throws Exception {
        PageHelper.startPage(pagenum, pagesize);
        List<Contact_user> list = webuserservice.userAll();
        PageInfo page = new PageInfo(list);
        return DtoUtil.returnDataSuccess(page);
    }

    @PutMapping(value = "/usermodify/{id}")
    @ApiOperation(value = "用户数据修改")
    @ResponseBody
    public Dto usermodify(@ApiParam(value = "json格式的修改参数") @RequestBody Contact_user contact_user, @PathVariable Integer id) throws Exception {
        contact_user.setId(id);
        Integer contactUser = webuserservice.usermodify(contact_user);
        if (contactUser == 1) {
            return DtoUtil.returnDataSuccess("true");
        }
        return DtoUtil.returnDataSuccess("false");
    }


    /*用户删除*/
    @DeleteMapping("/UserDelete/{xid}")
    public Dto UserDelete(@PathVariable("xid") Integer xid) throws Exception {
        Integer num = webuserservice.userdelete(xid);
        if (num == 1) {
            return DtoUtil.returnDataSuccess("true");
        }
        return DtoUtil.returnDataSuccess("false");
    }

    /*用户添加数据*/
    @PostMapping("/UserAdd")
    public Dto UserAdd(@RequestBody Contact_user contact_user) throws Exception {
        System.out.println("图片名称：" + contact_user.getContactavatar());
        contact_user.setContactdata(new Date());
        Integer nun = webuserservice.UserAdd(contact_user);
        if (nun == 1) {
            return DtoUtil.returnDataSuccess("true");
        }
        return DtoUtil.returnDataSuccess("false");
    }

    /*用户登录*/
    @PostMapping("/login")
    public Dto logo(@ApiParam("用户名") @RequestParam("contactalias") String contactalias, @ApiParam("密码") @RequestParam("password") String password, @ApiParam(hidden = true) @RequestHeader(value = "user-agent", required = false) String userAgent) throws Exception {

        //查询用户
        Contact_user user = webuserservice.UserLogin(contactalias);

        //用户登录
        String token= webuserservice.doLogin(user,password,userAgent);

        return DtoUtil.returnDataSuccess(token);
    }


}
